var express = require('express');
var router = express.Router();
var request = require('request');

var CommonJS = require('../../common/common');
var DBConfig = require('../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql);
var pool2 = mysql.createPool(DBConfig.mysql2);

// 复制原数据库中的商品
router.get('/jkyx/copy_goods', function(req, res, next) {
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.getGoods,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				// 处理商品
				var goods = results[0];
				var goods_len = goods.length;
				var saleNum = results[1];
				var saleNum_len = saleNum.length;
				var tpGoods = {};
				var tempGoods = [];
				var values = '';
				var values2 = '';
				var sqls = '';
				for (var i = 0; i < goods_len; i ++) {
					var goods_id = goods[i].id;
					var name = goods[i].name;
					var desc = goods[i].desc || '';
					var imgs = JSON.stringify(goods[i].imgs.split(','));
					var detail_imgs = goods[i].detail_imgs;
					var status = goods[i].status;
					var sort = goods[i].sort;
					var values = "'" + goods_id + "'" + ',' 
		  	  			 + "'" + name + "'" + ',' 
		  	  			 + "'" + desc + "'" + ',' 
		  	  			 + "'" + imgs + "'" + ','
		  	  			 + "'" + detail_imgs + "'" + ','
		  	             + "'" + status + "'" + ',' 
		  	             + "'" + sort + "'" + ',' 
		  	             + "'" + '1512007611' + "'" + ','
		  	             + "'" + '2017-11-30 10:6:51' + "'";
		  	        values = '(' + values + '),';
		  	        values2 = values2 + values;
				}
				values2 = values2.substring(0,(values2.length - 1));
				var inserSql = 'insert into goods (goods_id,name,descr,imgs,detail_imgs,status,sort,pre_sale_time,create_time) values ' + values2 + ';';
				pool2.getConnection(function(err, connection) {
					connection.query(inserSql,function (err, results) {
						connection.release();
						if (err){
						  	res.end(JSON.stringify({
				     			msg: '数据库查询失败',
				     			status:'102',
				     			err: err
				     		}));
						}else{
							res.end(JSON.stringify({
				     			msg: '操作成功',
				     			status:'100',
				     			goods: goods
				     		}));	      
						}
					})
				})   
			}
		})
	})
});
// 复制原数据库中的商品规格
router.get('/jkyx/copy_goods2', function(req, res, next) {
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query('select * from yymx_mallgoodssku where status = 0;select * from yymx_mallgoodsskuspec',function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
	     		var skus = results[0];
	     		var skus_len = skus.length;
	     		var specs = results[1];
	     		var specs_len = specs.length;
//	     		res.end(JSON.stringify({
//	     			msg: '数据库查询失败',
//	     			status:'102',
//	     			err: skus
//	     		}));
//	     		return;
	     		var sqlValue = ''
	     		for (var i = 0; i < skus_len; i ++ ) {
	     			for (var j = 0; j < specs_len; j ++) { 
	     				if(skus[i].id == specs[j].skuid){
	     					var goods_id = skus[i].spuid;
	     					var price = skus[i].price;
		     				var origprice = skus[i].origprice || 0;
		     				var spec = specs[j].specvalue;
		     				var salecount = skus[i].salecount;
		     				var erpcode = skus[i].erpcode;
		     				var stock = skus[i].stock;
		     				var values = "'" + goods_id + "'" + ',' 
			  	  			 + "'" + price + "'" + ',' 
			  	  			 + "'" + origprice + "'" + ',' 
			  	  			 + "'" + spec + "'" + ',' 
			  	  			 + "'" + salecount + "'" + ','
			  	  			 + "'" + erpcode + "'" + ','
			  	  			 + "'" + stock + "'" + ','
			  	             + "'" + '1' + "'" + ',' 
			  	             + "'" + '1' + "'" + ',' 
			  	             + "'" + '2017-11-30 16:24:51' + "'";
			  	            values = '(' + values + '),';
				  	        sqlValue = sqlValue + values;	
	     				}
	     			}
	     		}
	     		sqlValue = sqlValue.substring(0,(sqlValue.length - 1)); 
	  	        var inserSql = 'insert into goods_spec (goods_id,price,origprice,spec,salecount,erpcode,stock,status,sort,create_time) values ' + sqlValue;
				pool2.getConnection(function(err, connection) {
					connection.query(inserSql,function (err, results) {
						if (err){
						  	res.end(JSON.stringify({
				     			msg: '数据库查询失败',
				     			status:'102',
				     			err: err
				     		}));
						}else{
							res.end(JSON.stringify({
				     			msg: '操作成功',
				     			status:'100',
				     		}));	      
						}
					})
				})   
			}
		})
	})
});



module.exports = router;
